<?php
include("../include/db.php");
include("includes/thumbnail.php");

$_SESSION['rpage'] = $_SERVER['REQUEST_URI']; 
if(!isset($_SESSION[$prefix."_username"])){header("location:../login/");exit;}
if ($_SESSION[$prefix."_security"] < 10){header("location:../login/?error=2"); exit;}

$year = date("Y");
$date = "03/18/2011";


if ($_SERVER['REQUEST_METHOD'] == "POST") { 

	$title		= $_POST[title];
	$title_alpha= $_POST[title_alpha];
	$director	= $_POST[director];
	$time		= $_POST[runtime];
	$year		= $_POST[year];
	$country	= $_POST[country];
	$pictures	= $_POST[pics];
	$tickets	= $_POST[tickets];
	$video		= $_POST[video];
	$desc		= $_POST[editor1];
	$cat		= $_POST[cat];
	$web		= $_POST[web];

	if (($_FILES['mug_file']['name']) != ""){
		$site_pic_size = 400;
		$temp_loc = $_FILES['mug_file']['tmp_name'];
		$tn_name = basename($_FILES['mug_file']['name']);
		$file = "../files/$tn_name";
	$thumbnail = global_resize($temp_loc, $site_pic_size, $tn_name, $file);
	}
	//************************ INSERT INTO COURSES_TRM ********************************  
	$query = "INSERT INTO films 
				(title,
				title_alpha,
				description,
				country,
				year_produced,
				runtime,
				director,
				thumbnail,
				video_embed,
				pictures,
				ticket_link,
				film_web)
			 VALUES (
			 	'$title',
			 	'$title_alpha',
			 	'$desc',
			 	'$country',
			 	'$year',
			 	'$time',
			 	'$director',
			 	'$thumbnail',
			 	'$video',
			 	'$pictures',
			 	'$tickets',
			 	'$web'
			 )";
	$result = mysql_query($query, $db) or die(mysql_error());
	//********************************************************************************* 
$last_insert = mysql_insert_id($db);	
	if($cat != 'none'){
		//************************ INSERT INTO COURSES_TRM ********************************  
		$query = mysql_query("INSERT INTO film_cat (film_id, cat_id) VALUES ('$last_insert', '$cat')", $db);
		//*********************************************************************************
	}



	$venue		= $_POST[venue];
	$date		= $_POST[date];
	$hour		= $_POST[hour];
	$min		= $_POST[min];
	$ampm		= $_POST[ampm];

if($venue != 'none'){
	if($ampm == 'pm'){$hour = $hour + 12;}
	$time = "00:".$hour.":".$min;
	
	$date = date("Y-m-d", strtotime($date));

	//************************ INSERT INTO COURSES_TRM ********************************  
	$query = "INSERT INTO screenings 
				(film_id,
				date,
				time,
				venue_id,
				type)
			 VALUES (
			 	'$last_insert',
			 	'$date',
			 	'$time',
			 	'$venue',
			 	'f'
			 )";
	$result = mysql_query($query, $db) or die(mysql_error());
	//********************************************************************************* 
}


	
header("Location: film_list.php?action=added"); exit; 
}//END POST
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="includes/admin.css"/>
<title><?php echo $global_title;?></title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script type="text/javascript" src="../../ckeditor/ckeditor.js"></script>
<link rel="stylesheet" type="text/css" href="includes/jqueryui.css">
<script> 
	$(function() {
		$( "#date" ).datepicker();
	});
	</script> 
	
</head>
<body>

<div id="wrap">
	<div id="page-header">
		<?php include('includes/header.php');?>

	</div>
	
	<div id="page-body">
		<div id="tabs">
			<?php $tab="main"; include('includes/header_tabs.php');?>
		</div>

		<div id="acp">
		<div class="panel">
			<span class="corners-top"><span></span></span>
				<div id="content">
				<div id="menu">
						
					<?php include('includes/menu_top.php');?>	
					<?php include('includes/menu_main.php');?>
					</div>
	
					<div id="main">
<a name="maincontent"></a>


	<h1>Add Film</h1>

	<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" >
	
		<fieldset><legend>Add New Film</legend>

          <dl>
            <dt>Title:</dt>
            <dd><input type="text" name="title" id="title" required autofocus size="50"  /></dd>
          </dl>
          <dl>
            <dt>Alpha Title <small>(used to list the films in alphabetical order)</small>:</dt>
            <dd><input type="text" name="title_alpha" id="title_alpha" required size="50" /></dd>
          </dl>
          <dl>
            <dt>Director:</dt>
            <dd><input type="text" name="director" id="director"  size="50" /></dd>
          </dl>
          <dl>
            <dt>Running Time (minutes):</dt>
            <dd><input type="number" name="runtime" id="runtime" value="90"  /></dd>
          </dl>
          <dl>
            <dt>Year Produced:</dt>
            <dd><input type="text" name="year" id="year" value="<?php echo $year;?>"  /></dd>
          </dl>
          <dl>
            <dt>Country of Origin:</dt>
            <dd><input type="text" name="country" id="country" value="USA"  /></dd>
          </dl>
          <dl>
            <dt>Website:</dt>
            <dd><input type="url" name="web" id="web" placeholder="http://www...." size="50"  /></dd>
          </dl>
          <dl>
            <dt>Link to Pictures:</dt>
            <dd><input type="text" name="pics" id="pics" placeholder="http://www...." size="50"  /></dd>
          </dl>
          <dl>
            <dt>Link to Tickets:</dt>
            <dd><input type="text" name="tickets" id="tickets"  placeholder="http://www...." size="50"  /></dd>
          </dl>
                    
          <dl>
            <dt>Video Embed Code:</dt>
            <dd><textarea name="video" rows="6" cols="60"></textarea>
                </dd>
          </dl>
          
          
          <dl>
            <dt>Film Thumbnail Upload:</dt>
            <dd><input type="file" name="mug_file" id="mug_file"  /></dd>
          </dl>
          <dl>
				<dt>Add Film to Category:</dt>
			
				<dd>
				<select name="cat" id="cat">
                    <option value="none">None</option>
                    
                    <?php
                    
                    $result = mysql_query("SELECT * FROM categories  ORDER BY cat_name", $db);
                    	while ($myrow2 = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
                    	<option value="<?php echo $myrow2["cat_id"];?>" <?php if($myrow2["cat_id"] == $in_cat){echo "selected";}?>><?php echo $myrow2["cat_name"];?></option>

                    	<?php }?>

                  </select>
                  </dd>
			</dl>
		
		</fieldset>
		
 		<fieldset><legend>Add Screening</legend> 
			
			<dl>
				<dt>Venue:</dt>
			
				<dd>
				<select name="venue" id="venue">
                    <option value="none">None</option>
                    
                    <?php
                    
                    $result = mysql_query("SELECT * FROM venues  ORDER BY venue_name", $db);
                    	while ($myrow2 = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
                    	<option value="<?php echo $myrow2["venue_id"];?>" <?php if($myrow2["venue_id"] == $in_venue){echo "selected";}?>><?php echo $myrow2["venue_name"];?></option>

                    	<?php }?>

                  </select>
                 </dd>
               </dl>  
                  
          <dl>
            <dt>Date:</dt>
            <dd><input type="date" name="date" id="date" value="<?php echo $date;?>"   /></dd>
          </dl>
          
          <dl>
            <dt>Time:</dt>
            <dd><input type="number" name="hour" id="hour" step="1" min="1" max="12" placeholder="hour" size="4" maxlength="2" /> :
            	<input type="number" name="min" id="min" step="1" min="0" max="60" placeholder="min" size="4" maxlength="2"   />
            	<select name="ampm" id="ampm" >
            		<option value="am">AM</option>
            		<option value="pm" selected >PM</option>
            	</select>
            	</dd>
          </dl>

		</fieldset>
		
		<fieldset><legend>Film Description</legend> 


            <textarea name="editor1" id="editor1" class="ckeditor"></textarea>
         
      <p class="submit-buttons"><input type="submit" class="button1" name="submit" id="submit" value="Submit" /></p>
</fieldset>
    </form>	
		


    
		</div><!-- main -->
	</div><!-- content -->
<span class="corners-bottom"><span></span></span>
</div><!-- panel -->
</div><!-- acp -->

	
	<div id="page-footer">
	<?php include('includes/footer.php');?>
				</div>
</div><!-- page-body -->
</div><!-- wrap -->

   <script type="text/javascript">
	CKEDITOR.replace( 'editor1', {toolbar : 'MyToolbar'} );
	
	CKEDITOR.config.width = '700';
	CKEDITOR.config.height = '200';
	CKEDITOR.config.toolbarStartupExpanded = 'false';
	
</script>

</body> 
</html>